function addrFormatter(value, row, index){
    return row['proviceFirstStageName']+' ' + row['addressCitySecondStageName']+' '+row['addressCountiesThirdStageName']+' ' + row['addressDetailInfo'];
}

function titleFormatter(value, row, index){
    return '<a href="/item/pay?id='+row['item_id']+'">'+row['title']+'</a>'
}

function operateFormatter(value, row, index){
    return '<button class="weui_btn weui_btn_mini weui_btn_primary" onclick="operate('+row['id']+')">确定发货</button>'
}
function comFormatter(value, row, index){
    return '<input class="weui_input" id="'+'com_'+row['id']+'" type="text" placeholder="请输入快递公司" value="顺丰"></input>';
}
function expressFormatter(value, row, index){
    return '<input class="weui_input" id="'+'no_'+row['id']+'" type="text" placeholder="扫描快递单"><button class="weui_btn weui_btn_mini weui_btn_primary" onclick="scanCode()">扫一扫</button>'
}

function statusFormatter(value, row, index){
    if (value == 1){
        return '待发货';
    }
    else if(value == 2){
        return '已发货';
    }
}
function operate(id){
    com = $('#com_'+id).val();
    no = $('#no_'+id).val();
    if(com == "" || no == ""){
        sweetAlert("Oops...", "快递信息不完整", "error");
    }
    else{
        $.post('/deliver',{'express_no':no,'express_com':com,'id':id},function(data,status){
            if(data.data == 'suc'){
                swal("Good job!", "操作成功!", "success");
                $('#table1').bootstrapTable('refresh');
            }
        });
    }
}


function getTimeStamp() {
  var timestamp = new Date().getTime();
  return timestamp;
}
//得到随机字符串
function getNonceStr() {
  var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  var maxPos = $chars.length;
  var noceStr = "";
  for (i = 0; i < 32; i++) {
    noceStr += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  oldNonceStr = noceStr;
  return noceStr;
}
function getWXJSPAI(){
    var noceStr = getNonceStr();
    var ts = getTimeStamp();
    var jsapi_ticket;
    $.get('/getJSTicket',function(data, status){
        jsapi_ticket = data;
        url = 'http://'+window.location.host+window.location.pathname;
        forSign = 'jsapi_ticket='+jsapi_ticket+'&noncestr='+noceStr+'&timestamp='+ts+'&url='+url
        var shaObj = new jsSHA("SHA-1", "TEXT");
        shaObj.update(forSign);
        var signstring = shaObj.getHash("HEX");
        wx.config({
            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
            appId: 'wx5bf47679c84ef3c8', // 必填，公众号的唯一标识
            timestamp: ts, // 必填，生成签名的时间戳
            nonceStr: noceStr, // 必填，生成签名的随机串
            signature: signstring,// 必填，签名，见附录1
            jsApiList: ['scanQRCode'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        });

    });

}


function scanCode(){
    input = $(event.srcElement).prev();
    wx.scanQRCode({
    needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
    scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
    success: function (res) {
        var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
        if(result.split(',').length > 1)
            input.val(result.split(',')[1]);
        else
            input.val(result);
    }
    });
}


$(function(){
    getWXJSPAI();
    $('#table1').bootstrapTable({
    url: '/orderList',
    method: 'post',
    pagination: 'true',
    columns: [{
        field: 'id',
        title: 'ID',
        visible: false
    }, {
        field: 'userName',
        title: '收货人',
    }, {
        field: 'telNumber',
        title: '联系电话',
    }, {
        field: 'addr',
        title: '收货地址',
        formatter: addrFormatter
    }, {
        field: 'title',
        'title': '商品名称',
        formatter: titleFormatter,
    }, {
        field: 'quantity',
        title: '数量',
        sortable: 'true'
    }, {
        field: 'now_price',
        title: '单价(元)',
        sortable: 'true',
        visible: false
    }, {
        field: 'created_at',
        title: '购买时间',
        sortable: 'true'
    }, {
        field: 'status',
        title: '状态',
        formatter: statusFormatter,
    }, {
        field: 'express_com',
        title: '快递公司',
        formatter: comFormatter,
    }, {
        field: 'express_no',
        title: '快递单号',
        formatter: expressFormatter,
    }, {
        title: '操作',
        formatter: operateFormatter,
    }]
    });

    $('#table2').bootstrapTable({
    url: '/orderList?status=2',
    method: 'post',
    pagination: 'true',
    columns: [{
        field: 'id',
        title: 'ID',
        visible: false
    }, {
        field: 'userName',
        title: '收货人',
    }, {
        field: 'telNumber',
        title: '联系电话',
    }, {
        field: 'addr',
        title: '收货地址',
        formatter: addrFormatter
    }, {
        field: 'title',
        'title': '商品名称',
        formatter: titleFormatter,
    }, {
        field: 'quantity',
        title: '数量',
        sortable: 'true'
    }, {
        field: 'now_price',
        title: '单价(元)',
        sortable: 'true',
        visible: false
    }, {
        field: 'created_at',
        title: '购买时间',
        sortable: 'true'
    }, {
        field: 'delivered_at',
        title: '发货时间',
        sortable: 'true'
    }, {
        field: 'status',
        title: '状态',
        formatter: statusFormatter,
    }, {
        field: 'express_company',
        title: '快递公司',
    }, {
        field: 'express_no',
        title: '快递单号',
    }]
    });

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        $('#table1').bootstrapTable('refresh');
        $('#table2').bootstrapTable('refresh');
    })

});
